﻿#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    pApp=(QApplication*)QApplication::instance();
    QFile fp(":/style.qss");
    if(fp.open(QIODevice::ReadOnly|QIODevice::Text))
    {
        QTextStream fsty(&fp);
        QString qSty=fsty.readAll();
        pApp->setStyleSheet(qSty);
        fp.close();
    }
    setWindowFlag(Qt::FramelessWindowHint);
    setAttribute(Qt::WA_TranslucentBackground);
    QMovie *login_gif=new QMovie(":/login.gif");
    ui->label->setMovie(login_gif);
    ui->label->setStyleSheet("background:transparent");
    ui->label->setAttribute(Qt::WA_TransparentForMouseEvents);
    login_gif->start();
    ui->pushButton->setShortcut(QKeySequence("Ctrl+l"));
    QIcon exit(":/exit.png");
    ui->pushButton_3->setIcon(exit);
    ui->pushButton_3->setIconSize(ui->pushButton_3->size());
    ui->pushButton_3->setStyleSheet("border:none;background:none;");
    sql=new mySql;
    player=new QMediaPlayer(this);
    music_list=new QMediaPlaylist(player);
    QString playUrl = "qrc:/Sakura.mp3";
    music_list->addMedia(QUrl(playUrl));
    music_list->addMedia(QUrl("qrc:/yytz.mp3"));
    music_list->addMedia(QUrl("qrc:/xiaoge.mp3"));
    music_list->setPlaybackMode(QMediaPlaylist::Loop);
    player->setPlaylist(music_list);
    player->play();//背景音乐
    home=new myHome();
}

MainWindow::~MainWindow()
{
    sql->database.removeDatabase("QSQLITE");
    delete ui;
}




void MainWindow::on_pushButton_3_clicked()
{
    close();
}

void MainWindow::on_pushButton_2_clicked()
{
    QString account=ui->LineEdit->text();
    QString password=ui->LineEdit_2->text();
    if(account.isEmpty())
    {
        ui->LineEdit->setPlaceholderText("账号不能为空");
        return;
    }
    else if(password.isEmpty())
    {
        ui->LineEdit_2->setPlaceholderText("密码不能为空");
        return;
    }
    else//保存注册信息到数据库(不能重复注册)
    {
        if(!sql->openBase())
        {
            return;
        }
        QString command_1="CREATE TABLE IF NOT EXISTS userInfo(\
                          account TEXT,\
                          password TEXT)";
        sql->commonFuc(command_1);
        if(!sql->isRegistered(account))
        {
        command_1=QString("INSERT INTO userInfo VALUES('%1','%2')").\
                  arg(account).\
                  arg(password);
        sql->commonFuc(command_1);
        }
        else
        {
            ui->label_3->setText("!!!账号名字已经被注册了!!!");
        }
    }
    sql->closeBase();
}

void MainWindow::on_pushButton_clicked()
{
    QString account=ui->LineEdit->text();
    QString password=ui->LineEdit_2->text();
    if(account.isEmpty())
    {
        ui->LineEdit->setPlaceholderText("账号不能为空");
        return;
    }
    else if(password.isEmpty())
    {
        ui->LineEdit_2->setPlaceholderText("密码不能为空");
        return;
    }
    else
    {
        if(!sql->openBase())
        {
            return;
        }
        if(sql->isRegistered(account))//账号注册了
        {
            if(sql->isPassword(account,password))
            {
                //登录成功
                qDebug("登录成功");
                home->backWidget(this);
                home->show();
                this->hide();
                //player->pause();
            }
            else
            {
                ui->label_3->setText("密码错误");
            }
        }
        else
        {
            ui->label_3->setText("!!!账号未注册");
        }
    }
    sql->closeBase();
}
